package com.hanock.sm.fork2d;

import android.graphics.Rect;
import android.graphics.RectF;

public class Sprite extends Node {

	private int resId;
	private Rect srcRect;
	private RectF desRect;
	private boolean fullSrcRect;
	private boolean fitCenter;
	private boolean fitLeft;
	
	
	public Sprite() {
		this(0);
	}
	
	/**
	 * 
	 * @param resId - zero for empty sprite
	 */
	public Sprite(int resId) {
		this.resId = resId;
		this.srcRect = null;
		
		fullSrcRect = true;
		
		srcRect = new Rect();
		desRect = new RectF();
	}
		
	public boolean isFitCenter() {
		return fitCenter;
	}
	
	public void setFitCenter(boolean fitCenter) {
		this.fitCenter = fitCenter;
	}
	
	public void setFitLeft(boolean fitLeft) {
		this.fitLeft = fitLeft;
	}
	
	public void setResId(int resId) {
		this.resId = resId;
	}
	
	public void setSrcRect(Rect rect){
		setSrcRect(rect.left, rect.top, rect.right, rect.bottom);
	}
	
	public void setSrcRect(int left, int top, int right, int bottom){
		srcRect.set(left, top, right, bottom);
		fullSrcRect = false;
	}
	
	public void setSrcRectFull(){
		fullSrcRect = true;
	}
	
	public Rect getSrcRect(boolean useSrcRect){
		fullSrcRect = false;
		return srcRect;
	}
	
	@Override
	public void setWidth(float width) {
		super.setWidth(width);
	}
	
	@Override
	public void setHeight(float height) {
		super.setHeight(height);
	}
	
	@Override
	public void setSize(float width, float height) {
		super.setSize(width, height);
	}
	
	@Override
	public void draw(Canvas canvas) {
		
		if (resId == 0)
			return;
		
		getDesRect(desRect);
		Texture texture = TextureManager.getInstance().load(resId);
		
		if (fitCenter){
			if (fullSrcRect){
				TextureUtil.fitCenter(texture, null, desRect);
			}
			else{
				TextureUtil.fitCenter(texture, srcRect, desRect);
			}
		}
		else if (fitLeft){
			if (fullSrcRect){
				TextureUtil.fitLeft(texture, null, desRect);
			}
			else{
				TextureUtil.fitLeft(texture, srcRect, desRect);
			}
		}
		
		if (fullSrcRect)
			canvas.drawImage(null, desRect, texture);
		else
			canvas.drawImage(srcRect, desRect, texture);
	}

}















